package com.artron.mediaartron.data.task;

import android.util.Log;

import com.artron.baselib.entity.Response;
import com.artron.mediaartron.data.entity.Works;
import com.artron.mediaartron.data.net.NetConstant;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.google.gson.Gson;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
import rx.Observable;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-10-31.
 */

public class AddShoppingCartTask {

    private static final String TAG = "AddShoppingCartTask";

    public static Observable<Response> addShoppingCart(Works works) {
        Gson gson = RetrofitHelper.buildJsonParser();
        final String jsonStr = gson.toJson(works);
        Log.d(TAG, jsonStr);
        RequestBody body = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse("application/json");
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                BufferedInputStream bufIs = new BufferedInputStream(new ByteArrayInputStream(jsonStr.getBytes()));
                byte[] buf = new byte[1024 * 100];
                int len;
                while ((len = bufIs.read(buf)) != -1) {
                    sink.write(buf, 0, len);
                    sink.flush();
                }
                bufIs.close();
            }
        };
        return RetrofitHelper.getOrderApi().addShoppingCart(body);
    }
}
